package com.mianyang.zhangsan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mianyang.zhangsan.common.constant.SecConstant;
import com.mianyang.zhangsan.common.util.CacheHelper;
import com.mianyang.zhangsan.common.util.StringUtils;
import com.mianyang.zhangsan.model.entity.SecStaticData;
import com.mianyang.zhangsan.service.SecStaticDataService;
import com.mianyang.zhangsan.mapper.SecStaticDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author jiangjr
*/
@Slf4j
@Service
public class SecStaticDataServiceImpl extends ServiceImpl<SecStaticDataMapper, SecStaticData>
    implements SecStaticDataService{

    @Autowired
    private CacheHelper cacheHelper;

    // 项目启动时刷新缓存
    @PostConstruct
    public void init() {
       log.info("=============静态配置缓存刷新开始===================");
       this.refresh();
       log.info("=============静态配置缓存刷新成功===================");
    }


    // todo 部分刷新
    @Override
    public void refresh() {
        Map<String, Object> cache = new HashMap<>();
        List<SecStaticData> staticDataList = list();
        if (staticDataList != null && staticDataList.size() > 0) {
            for (SecStaticData staticData : staticDataList) {
                String codeType = staticData.getCodeType();
                String codeValue = staticData.getCodeValue();
                String singleton = cacheHelper.cacheKey(codeType, codeValue);
                cache.put(singleton, staticData);
                String prototype = cacheHelper.cacheKey(codeType);
                addOrAppend(cache, prototype, staticData);
            }
        }
        cacheHelper.addValue(SecConstant.CACHE_DATA_LABEL, cache);
    }

    @Override
    public List<SecStaticData> getCache(String codeType, String codeValue) {
        Map<String, Object> cache = (Map<String, Object>) cacheHelper.getValue(SecConstant.CACHE_DATA_LABEL);
        String cacheKey = "";
        List<SecStaticData> staticDataList = new ArrayList<>();
        // 1.从缓存中查询数据
        if (StringUtils.isNotEmpty(codeValue)) {
            cacheKey = cacheHelper.cacheKey(codeType, codeValue);
            SecStaticData staticData = (SecStaticData) cache.get(cacheKey);
            if (staticData != null) {
                staticDataList.add(staticData);
            }
        } else {
            cacheKey = cacheHelper.cacheKey(codeType);
            List<SecStaticData> list = (List<SecStaticData>) cache.get(cacheKey);
            if (list != null && list.size() > 0) {
                staticDataList.addAll(list);
            }
        }
        if (staticDataList.size() <= 0) {
            // 2.缓存中不存在则直接查询数据库
            QueryWrapper<SecStaticData> wrapper = new QueryWrapper<>();
            wrapper.eq("code_type", codeType);
            if (StringUtils.isNotEmpty(codeValue)) {
                wrapper.eq("code_value", codeValue);
            }
            List<SecStaticData> list = list(wrapper);
            if (list != null && list.size() > 0) {
                staticDataList.addAll(list);
                // 3.同步缓存
                this.refresh();
            }
        }
        return staticDataList;
    }

    public void addOrAppend(Map<String, Object> cache, String key, SecStaticData value) {
        List<SecStaticData> staticDataList = (List<SecStaticData>) cache.getOrDefault(key, new ArrayList<SecStaticData>());
        staticDataList.add(value);
        cache.put(key, staticDataList);
    }
}




